/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernatetests1;

import java.util.Iterator;
import my.hibernate.Book;
import my.hibernate.BookCategory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author Manu
 */
public class HibernateTests1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Initializing hibernate and getting the SessionFactory ...");
        Configuration configuration = new AnnotationConfiguration().configure();
        SessionFactory sessionFactory = configuration.buildSessionFactory();

        Session session = sessionFactory.getCurrentSession();

        session.beginTransaction();

        //***example 1: we want to create a book_category and a book that belongs to this category
//        Book book=new Book();
//        book.setTitle("Bienvenue chez les Schtroumpfs");
//        book.setSynopsis("Une histoire de Schtroumpfs comme il en existe tant ...");
//        
//        Book_Category category=new Book_Category();
//        category.setName("enfants");
//        category.setDescription("Des livres pour enfants");
//        category.getBooks().add(book);
//        
//         
//        session.saveOrUpdate(category);
//        
         //***example 2: we want to fetch a book_category, to verify if we get the list of books within it.            
        Query query=session.createQuery("from BookCategory where name='enfants'");
        BookCategory bookCategory=(BookCategory)query.uniqueResult();
        
        System.out.println("Number of books in this category:"+bookCategory.getBooks().size());
        System.out.println("First book in this category is \'"+((Book)bookCategory.getBooks().iterator().next()).getTitle()+"\'");
               
        //***example 3: now checking the other way around
        query=session.createQuery("from Book where title LIKE '%Schtroump%'");
        Book book=(Book)query.uniqueResult();
        
        System.out.println("Book title '"+book.getTitle()+"'");
        System.out.println("Found "+book.getCategories().size()+" categories:");
        
        Iterator it=book.getCategories().iterator();
        
        while (it.hasNext()) {
            System.out.println("'"+((BookCategory)it.next()).getName()+"'");
        }
        
        
        bookCategory.setBooks(null);
        book.setCategories(null);
        
        session.flush();
            
        //does it erase the categories too ?
        session.delete(book);
        
//        BookCategory category=new BookCategory();
//        category.setName("B.D.");
//        category.setDescription("Des bandes dessinées");
//        category.getBooks().add(book);
//        
//        session.saveOrUpdate(category);
        
        session.getTransaction().commit();

    }
}
